package com.flymobi.admin;

import java.io.File;
import java.util.Calendar;
import java.util.List;

import com.flymobi.fw.modules.SM;
import com.flymobi.fw.util.DateUtil;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.Manager;
import com.flymobi.po.SmsLog;
import com.flymobi.po.SmsTask;
import com.flymobi.util.C;
import com.flymobi.util.Pagination;
import com.flymobi.util.WebContext;
import com.flymobi.vo.SmsLogSearchVo;
import com.flymobi.vo.SmsTaskSearchVo;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Oct 21, 2008 11:16:42 AM
 * @version $Id: ManagerAction.java,v 1.1 2010/04/09 03:27:08 airsou Exp $
 */
public class SmsTaskAction extends WebAction {
	private SmsTask smstask;
	
	/**
	 * @return the smstask
	 */
	public SmsTask getSmstask() {
		return smstask;
	}

	/**
	 * @param smstask the smstask to set
	 */
	public void setSmstask(SmsTask smstask) {
		this.smstask = smstask;
	}

	private String s;
	private String e;
	
	/**
	 * @return the s
	 */
	public String getS() {
		return s;
	}

	/**
	 * @param s
	 *            the s to set
	 */
	public void setS(String s) {
		this.s = s;
	}

	/**
	 * @return the e
	 */
	public String getE() {
		return e;
	}

	/**
	 * @param e
	 *            the e to set
	 */
	public void setE(String e) {
		this.e = e;
	}

	private SmsTaskSearchVo searchparam;

	/**
	 * @return the searchparam
	 */
	public SmsTaskSearchVo getSearchparam() {
		return searchparam;
	}

	/**
	 * @param searchparam
	 *            the searchparam to set
	 */
	public void setSearchparam(SmsTaskSearchVo searchparam) {
		this.searchparam = searchparam;
	}

	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String webProcess(WebContext context, Manager manager) {
		if (searchparam == null) {
			searchparam = new SmsTaskSearchVo();
			searchparam.setStatus(-1);
			searchparam.setShenhe(-1);
		}
		if ("add".equals(method)) {
			return "edit";
		}else if ("edit".equals(method)) {
			smstask = SM.SmsTaskService.get(id);
			return "edit";
		} else if ("delete".equals(method)) {
			SM.SmsTaskService.delete(id);
			context.put("rmsg", C.TEXT.DELETE_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("save".equals(method)) {
			if(smstask.getId()!=null){
				SmsTask u=SM.SmsTaskService.get(smstask.getId().toString());
				u.setName(smstask.getName());
				u.setMessage(smstask.getMessage());
				u.setShenhe(smstask.getShenhe());
				u.setStatus(smstask.getStatus());
				smstask=u;
			}else{
				smstask.setShenhe(0);
				smstask.setStatus(0);
				smstask.setCreator(manager.getName());
				smstask.setCreatorType("1");
				smstask.setManagerId(manager.getId());
			}
			SM.SmsTaskService.save(this.smstask);
			for (int i = 0; i < upload.size(); i++) {
				File file = upload.get(i);
				SM.SmsLogService.save(smstask,file);
			}
			context.put("rmsg", C.TEXT.SAVE_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		}else if ("deleteselect".equals(method)) {
			for (String item : selItems) {
				SM.SmsTaskService.delete(item);
			}
			context.put("rmsg", C.TEXT.DELETE_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("passselect".equals(method)) {
			for (String item : selItems) {
				SmsTask c=SM.SmsTaskService.get(item);
				c.setShenhe(1);
				SM.SmsTaskService.save(c);
				SmsLogSearchVo param = new SmsLogSearchVo();
				param.setTaskId(c.getId());
				List<SmsLog> mobileList = SM.SmsLogService.list(param, 0, 10000);
				if(mobileList!=null){
					for(SmsLog smsLog:mobileList){
						smsLog.setShenhe(c.getShenhe());
						SM.SmsLogService.save(smsLog);
					}
				}
			}
			context.put("rmsg", C.TEXT.OPERA_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("rejectselect".equals(method)) {
			for (String item : selItems) {
				SmsTask c=SM.SmsTaskService.get(item);
				c.setShenhe(2L);
				SM.SmsTaskService.save(c);
				SmsLogSearchVo param = new SmsLogSearchVo();
				param.setTaskId(c.getId());
				List<SmsLog> mobileList = SM.SmsLogService.list(param, 0, 10000);
				if(mobileList!=null){
					for(SmsLog smsLog:mobileList){
						smsLog.setShenhe(c.getShenhe());
						SM.SmsLogService.save(smsLog);
					}
				}
			}
			context.put("rmsg", C.TEXT.OPERA_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("list".equals(method)) {
			if (!StringUtil.isEmpty(s)) {
				searchparam.setStart(DateUtil.parseStrDate(s,
						"yyyy-MM-dd"));
			}
			if (!StringUtil.isEmpty(e)) {
				searchparam.setEnd(DateUtil.parseStrDate(e,
						"yyyy-MM-dd"));
			}
			if (searchparam == null) {
				searchparam = new SmsTaskSearchVo();
				searchparam.setStatus(-1);
				searchparam.setShenhe(-1);
			}
			if (searchparam.getStart() == null) {
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				searchparam.setStart(calendar.getTime());
			}
			if (searchparam.getEnd() == null) {
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				searchparam.setEnd(calendar.getTime());
			}
			List<SmsTask> smsTaskList = SM.SmsTaskService.list(searchparam, startRow,
					pagePerSize);
			context.put("smsTaskList", smsTaskList);
			Pagination pagination = new Pagination(SM.SmsTaskService.getCount(searchparam)
					.intValue(), startRow, pagePerSize);
			context.put("pagination", pagination);
			return "list";
		}else if ("mylist".equals(method)) {
			if (!StringUtil.isEmpty(s)) {
				searchparam.setStart(DateUtil.parseStrDate(s,
						"yyyy-MM-dd"));
			}
			if (!StringUtil.isEmpty(e)) {
				searchparam.setEnd(DateUtil.parseStrDate(e,
						"yyyy-MM-dd"));
			}
			if (searchparam == null) {
				searchparam = new SmsTaskSearchVo();
				searchparam.setStatus(-1);
				searchparam.setShenhe(-1);
			}
			if (searchparam.getStart() == null) {
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				searchparam.setStart(calendar.getTime());
			}
			if (searchparam.getEnd() == null) {
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				searchparam.setEnd(calendar.getTime());
			}
			searchparam.setManagerId(manager.getId());
			List<SmsTask> smsTaskList = SM.SmsTaskService.list(searchparam, startRow,
					pagePerSize);
			context.put("smsTaskList", smsTaskList);
			Pagination pagination = new Pagination(SM.SmsTaskService.getCount(searchparam)
					.intValue(), startRow, pagePerSize);
			context.put("pagination", pagination);
			return "mylist";
		}else {
			method = "list";
			return this.webProcess(context, manager);
		}
	}
}
